/**
 * Created by caiqyxyx on 2017/2/12.
 */
define(['durandal/app', 'durandal/composition', 'i18nCommon', 'http', 'jquery', 'knockout', 'bootstrap', 'cyworkUI', 'BMap', 'bootbox','websocket',
        'css!style/home/page-style',
        'css!library/3rdparty/font-awesome/css/font-awesome.min'],
    function (app, composition, i18n, http, $, ko, boots, UI, baiduMap, Box,websocket) {
        var home = new function () {
            var that = this;
            that.init = function () {
                composition.addBindingHandler('application-home', {
                    init: function (dom) {
                        that._dom = dom;
                        that.bindUI();
                        that.renderUI();

                    }
                })
            };
            that.bindUI = function () {
                /*初始化导航栏*/
                var getNav = http.getInstance("home/nav/getHumanNavItem", {type: "POST"});
                getNav.ajax().then(function (data, result) {
                    // if(data.overTimeRightFlag){
                    //     websocket.init();//初始化websocket,前台设置是否建立websocket连接
                    // }
                    if(data.overTimeRecNum !== 0 ){
                        Box.alert({
                            title: "提醒",
                            size: "small",
                            message: "您有"+data.overTimeRecNum+"条超时案件需要处理!"
                        });
                    }else{
                        Box.alert({
                            title: "提醒",
                            size: "small",
                            message: "您暂时没有超时案件需要处理!"
                        });
                    }
                    UI.initUI('.side-menu', data.item, data.group);
                }).error();

                /**
                 * 菜单收缩
                 */
                $(that._dom).delegate(".layout-side-arrow", "click", function () {
                    if ($(".layout-side").hasClass("close")) {
                        $(".layout-side").removeClass("close");
                        $(".layout-main").removeClass("full-page");
                        $(".layout-footer").removeClass("full-page");
                        $(this).removeClass("close");
                        $(".layout-side-arrow-icon").removeClass("close");
                        model.arrowSwitch('<i class="icon-caret-left"></i>');
                    } else {
                        $(".layout-side").addClass("close");
                        $(".layout-main").addClass("full-page");
                        $(".layout-footer").addClass("full-page");
                        $(this).addClass("close");
                        $(".layout-side-arrow-icon").addClass("close");
                        model.arrowSwitch('<i class="icon-caret-right"></i>');
                    }
                });

                model.logoutSystem = function () {
                    var logoutService = http.getInstance("logout");
                    Box.confirm({
                        title: "提醒",
                        size: "small",
                        message: "确定要退出系统吗？",
                        callback: function (result) {
                            if (result) {
                                logoutService.ajax().then(function () {
                                    window.location = cywork.global.rootPath + 'main' + window.location.search;
                                });
                            }
                        }
                    });
                };

            };

            that.renderUI = function () {
                that.initMap();
            };

            /**
             * 初始化地图实例
             */
            that.initMap = function () {
                var map = new BMap.Map("application-map",{enableMapClick: false});
                var point = new BMap.Point(116.331398, 39.897445);
                map.centerAndZoom(point, 12);
                map.enableScrollWheelZoom();


                var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角，添加比例尺
                var top_left_navigation = new BMap.NavigationControl();

              //  var geoc = new BMap.Geocoder();

                map.addControl(top_left_control);
                map.addControl(top_left_navigation);

                /*根据IP自动定位
                 var myCity = new BMap.LocalCity();
                 myCity.get(function (result) {
                 map.setCenter(result.name);
                 });*/
                /*地址解析定位*/
                // 创建地址解析器实例
                var myGeo = new BMap.Geocoder();
                // 将地址解析结果显示在地图上,并调整地图视野
                myGeo.getPoint("宁波市江东区", function (_point) {
                    if (_point) {
                        map.centerAndZoom(_point, 16);
                        //map.addOverlay(new BMap.Marker(_point));
                    } else {
                        // alert("您选择地址没有解析到结果!");
                    }
                }, "宁波市");
            };
        };

        home.init();

        var model = {
            textSystemDisplayName: ko.observable(i18n.textSystemDisplayName),
            textSystemLocation: ko.observable(i18n.textSystemLocation),
            textSystemRightDisplayName: ko.observable(''),
            humanName: ko.observable('<i class="icon-user" style="margin-left:5px;"></i>  ' + cywork.global.humanName),
            arrowSwitch: ko.observable('<i class="icon-caret-left"></i>'),
            logoutSystem: ko.observable(),
            textDevUnit: ko.observable(i18n.textSystemDevTeam),
            humanSettingHandler: ko.observable(),
            humanInfoHandler: ko.observable(),
            logoutHandler: ko.observable()
        };
        return model;

    });